package com.king.headline.view;

/**
 * Created by king on 2016/5/18.
 * 自定义圆角矩形
 */
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.RoundRectShape;

import com.king.headline.util.UIUtils;


public class RoundRectDradable extends Drawable{
    private static final float DEFAULT_RADIUS = UIUtils.dip2px(10);
    private Paint mPaint = new Paint();
    private RoundRectShape mShape;
    private float[] mOuter;
    private int mColor;
    private int mPressColor;
    private float mTopLeftRadius = DEFAULT_RADIUS;
    private float mTopRightRadius = DEFAULT_RADIUS;
    private float mBottomLeftRadius = DEFAULT_RADIUS;
    private float mBottomRightRadius = DEFAULT_RADIUS;
    public RoundRectDradable() {
        mColor = Color.WHITE;
        mPressColor = Color.WHITE;
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }

    /**
     * 设置圆角的角度
     * @param mTopLeftRadius
     * @param mTopRightRadius
     * @param mBottomLeftRadius
     * @param mBottomRightRadius
     */
    public void setRadius(float mTopLeftRadius,float mTopRightRadius,float mBottomLeftRadius,float mBottomRightRadius){
        this.mTopLeftRadius = mTopLeftRadius;
        this.mTopRightRadius = mTopRightRadius;
        this.mBottomLeftRadius = mBottomLeftRadius;
        this.mBottomRightRadius = mBottomRightRadius;
    }

    /**
     * 刷新绘图
     */
    private void refreshShape(){
        mOuter = new float[]{mTopLeftRadius, mTopLeftRadius//左上
                , mTopRightRadius, mTopRightRadius//右上
                , mBottomRightRadius, mBottomRightRadius//右下
                , mBottomLeftRadius, mBottomLeftRadius};//左下
        mShape = new RoundRectShape(mOuter, null, null);
    }
    public void setColor(int color){
        mColor = color;
        mPaint.setColor(color);
    }


    public float getTopLeftRadius() {
        return mTopLeftRadius;
    }
    public float getTopRightRadius() {
        return mTopRightRadius;
    }
    public float getBottomLeftRadius() {
        return mBottomLeftRadius;
    }
    public float getBottomRightRadius() {
        return mBottomRightRadius;
    }
    public int getPressColor() {
        return mPressColor;
    }
    public void setPressColor(int pressColor) {
        this.mPressColor = pressColor;
    }
    @Override
    public void draw(Canvas canvas) {
        mShape.draw(canvas, mPaint);
    }
    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }
    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }
    @Override
    public int getOpacity() {
        return mPaint.getAlpha();
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        refreshShape();
        mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top);
    }

}